package com.gimi.cloud.jimi.client.controller;

import com.gimi.cloud.jimi.client.service.order.OrderDetailService;
import com.gimi.cloud.jimi.client.service.order.OrderService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.order.ReplantDTO;
import com.gimi.cloud.jimi.dto.params.OrderParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by shazhiwei on 2018/1/9.
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;
    private final OrderDetailService orderDetailService;
    private static Logger logger = LoggerFactory.getLogger(OrderController.class);

    public OrderController(OrderService orderService,OrderDetailService orderDetailService){
        this.orderService = orderService;
        this.orderDetailService = orderDetailService;
    }

    /**
     * 生成订单
     */
    @RequestMapping(value = "/addOrder")
    public void addOrder(@RequestBody OrderParams orderParams, HttpServletResponse response) {
        orderService.addOrder(orderParams);
        Response resp = new Response();
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 判断资料有效期
     */
    @RequestMapping(value = "/getDataStatus")
    public void getDataStatus(@RequestBody OrderParams orderParams, HttpServletResponse response) {
        //TODO:这个接口可以加一些缓存
        Map<String,Object> data = orderService.getDataStatus(orderParams);
        Response resp = new Response();
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 用户租赁订单
     */
    @RequestMapping(value = "/getOrderList")
    public void getOrderList(@RequestBody OrderParams orderParams, HttpServletResponse response) throws Exception {
        List<Map> data = orderService.getOrderList(orderParams);
        Response resp = new Response();
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 商品详情
     */
    @RequestMapping(value = "/getOrderDetail")
    public void getOrderDetail(@RequestBody OrderParams orderParams, HttpServletResponse response) throws Exception {
    	logger.info("请求参数==>"+orderParams);
    	Response resp = new Response();
    	Map<String, Object> map = orderDetailService.getOrderDetail(orderParams);
    	resp.setRes_data(map);
    	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
    	Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 租赁订单-租费账期
     */
    @RequestMapping(value = "/getReplantDetail")
    public void getReplantDetail(@RequestBody ReplantDTO replantDTO, HttpServletResponse response) throws Exception {
        logger.info("请求参数==>"+replantDTO);
        Response resp = new Response();
        List<ReplantDTO> replantDTOList= orderDetailService.getReplantDetail(replantDTO);
        resp.setRes_data(replantDTOList);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }
}
